//
//  UIColor+Extensions.swift
//  SpeechAnalyzerDemo
//
//  替换原有 SwiftUI 的颜色工具方法。
//

import UIKit

extension UIColor {
    static var redGray: UIColor {
        UIColor.systemRed.withAlphaComponent(0.9).mixed(withFraction: 0.1, color: .systemGray)
    }

    // 提供一个混合颜色的小工具，模拟 SwiftUI 的 mix(with:by:) 行为。
    private func mixed(withFraction fraction: CGFloat, color: UIColor) -> UIColor {
        let fraction = max(0, min(1, fraction))

        var r1: CGFloat = 0
        var g1: CGFloat = 0
        var b1: CGFloat = 0
        var a1: CGFloat = 0
        guard getRed(&r1, green: &g1, blue: &b1, alpha: &a1) else { return self }

        var r2: CGFloat = 0
        var g2: CGFloat = 0
        var b2: CGFloat = 0
        var a2: CGFloat = 0
        guard color.getRed(&r2, green: &g2, blue: &b2, alpha: &a2) else { return self }

        let red = r1 * (1 - fraction) + r2 * fraction
        let green = g1 * (1 - fraction) + g2 * fraction
        let blue = b1 * (1 - fraction) + b2 * fraction
        let alpha = a1 * (1 - fraction) + a2 * fraction

        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
}
